<div v-cloak id="app" class="main-content">
    <div class="form-body">
        <one-form :config="config" v-model="fdata" ref="ruleForm">
            <div slot="menuLeft">
                <el-button
                    class="button-item"
                    :loading="btnLoading"
                    type="primary"
                    @click="store('ruleForm')"
                    size="small"
                >
                    保存
                </el-button>
                <el-button size="small" @click="backpage()">返回</el-button>
            </div>
            <!-- 上传图片 -->
            <template slot="img" slot-scope="scope">
                <one-img-cell
                    v-model="fdata[scope.row.prop]"
                    :img-cell-item-style="{width:'100px',height: '100px',}"
                    :max="1"
                ></one-img-cell>
            </template>
        </one-form>
    </div>
</div>
{include file='common@components/one-form'} {include
    file='common@components/one-img-cell'}
<script>
    const _formData = JSON.parse('{:json_encode((array)$formData, 1)}');
    const app = new Vue({
        el: '#app',
        data() {
            const imgValidator = (rule, value, callback) => {
                if (this.fdata.isad <= 1 && this.fdata.img == '') {
                    callback(new Error('当前类型下图片不能为空'));
                } else {
                    callback();
                }
            };
            const adUrlValidator = (rule, value, callback) => {
                if (this.fdata.isad > 1) {
                    if (this.fdata.ad_url == '') {
                        callback(
                            new Error('当前类型下广告链接|小程序广告id不能为空')
                        );
                    } else {
                        callback();
                    }
                } else {
                    callback();
                }
            };
            return {
                uploading: false,
                loading: false,
                btnLoading: false,
                config: {
                    formdesc: [
                        {
                            label: '轮播图类型 :',
                            prop: 'isad',
                            type: 'select',
                            rules: [
                                { required: true, message: '请选择轮播图类型' },
                            ],
                            options: [
                                {
                                    label: '普通图片',
                                    value: 0,
                                },
                                {
                                    label: '自定义链接广告',
                                    value: 1,
                                },
                                {
                                    label: '小程序BANNER广告',
                                    value: 2,
                                },
                            ],
                        },
                        {
                            label: '标题 :',
                            prop: 'title',
                            type: 'input',
                            rules: [
                                { required: true, message: '标题不能为空' },
                            ],
                            bind: {
                                placeholder: '请输入标题',
                            },
                        },
                        {
                            label: '图片 :',
                            prop: 'img',
                            type: 'image',
                            content: '类型为普通图片或自定义链接广告时必填',
                            rules: [
                                {
                                    validator: imgValidator,
                                },
                            ],
                            slot: true
                        },
                        {
                            label: '远程图片 :',
                            prop: 'img',
                            type: 'input',
                            content:
                                '类型为普通图片或自定义链接广告时必填，填写此项后上面上传的图片无效',
                            rules: [
                                {
                                    validator: imgValidator,
                                },
                            ],
                        },
                        {
                            label: '广告链接|小程序广告id :',
                            prop: 'ad_url',
                            type: 'input',
                            content:
                                '类型为自定义链接广告或小程序BANNER广告时此项必填<br>*小程序广告id在小程序流量主广告内创建banner广告获取',
                            rules: [
                                {
                                    validator: adUrlValidator,
                                },
                            ],
                        },
                        {
                            label: '视频id :',
                            prop: 'vid',
                            type: 'input',
                            content:
                                '类型为普通图片时生效必填，在cms系统-视频管理中获取',
                        },
                    ],
                    labelWidth: '200px',
                    rowSize: 1, //一行可以展示几列表单，默认为3列
                },
                fdata: {
                    id: 0,
                    title: '',
                    isad: 0,
                    vid: 0,
                    img: '',
                    ad_url: '',
                },
            };
        },
        created() {},
        mounted() {
            if (Object.keys(_formData).length > 0) {
                this.fdata = _formData;
            }
        },
        methods: {
            //表单验证
            getFormPromise(form) {
                return new Promise((resolve) => {
                    form.validate((res) => {
                        resolve(res);
                    });
                });
            },
            store(formName) {
                let _this = this;
                // 获取到组件中的form
                const configForm = this.$refs.ruleForm.$refs.ruleForm;
                // 使用Promise.all去校验结果,可加入多个表单
                Promise.all([configForm].map(this.getFormPromise)).then(
                    (res) => {
                        const validateResult = res.every((item) => !!item);
                        if (validateResult) {
                            _this.loading = true;
                            request({
                                params: {
                                    s: 'banner/banner/edit',
                                },
                                method: 'post',
                                data: _this.fdata,
                            }).then((e) => {
                                _this.loading = false;
                                if (e.data.code == 0) {
                                    _this.backpage();
                                } else {
                                    _this.$message.error(e.data.msg);
                                }
                            });
                        } else {
                            console.log('表单未校验通过');
                        }
                    }
                );
            },
            backpage() {
                navigateTo({
                    s: 'banner/banner/index',
                });
            },
            //图片上传完成
            uploadSuccess(file, prop) {
                file = file.response.data.data.file;
                this.fdata[prop] = file;
            },
        },
    });
</script>
